//渠道
ascription2()
function ascription2() {
    $.ajax({
        type: "post",
        url: root.dockingplatform.allChannelConfig,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {

        },
        success: function (data) {
            console.log(data)
            if(data.errorCode=="0") {
                var dataInter=data.data;
                var interText = doT.template($("#ascription_bob").text());
                $("#ascription_lsit").html(interText(dataInter));
            }
            if (sessionStorage.getItem("exception")!=null){
                $("#exceptionType").val(sessionStorage.getItem("exception"));
                $("#ascription_lsit").val(sessionStorage.getItem("customerCode"));
                sessionStorage.removeItem("exception");
                sessionStorage.removeItem("customerCode");
                getList()
            }
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });


}

/*表格渲染*/
var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];

function orderList(data) {
    console.log(data)
    dataSource.splice(0, dataSource.length);//清空数组
if(data.data!=null){
    $.each(data.data.listFailedPreOrder, function (i, field) {
        var rowData = new Array();
        rowData.push(field.parentCustomerName);
        rowData.push(field.channelOrderId);
        rowData.push(field.orderCreatedAt);
        rowData.push(field.pushAt);
        rowData.push(field.reason);
        dataSource.push(rowData);
    });
}

    initDataTable(dataSource);

    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "order": [[ 1, "desc" ]],
            "autoWidth": false,
            "ordering": false,
            "searching": true,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [5, 10, 25, 50],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />",
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },

                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets":2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[3]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets":3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[4]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets":4
                },
                {
                    "render": function (data, type, row) {
                        var val = '<span class=""><a onclick="applycreate(\''+row[1]+'\',this)">申请生成</a></span><span>&nbsp&nbsp<a onclick="cancel(\''+row[1]+'\')">取消</a></span>';
                        return val;
                    },
                    "targets": 5
                },
            ]
        });
        //权限控制函数
        //power()
    }
}


//申请生成
function applycreate(channelOrderId) {
    $.ajax({
        url:root.dockingplatform.rerunPreOrder,
        contentType: "application/json;charset=UTF-8",
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        data: JSON.stringify({"channelOrderId":channelOrderId}),
        success: function (data) {
            if(data.errorCode==0){
                $.Huimodalalert('申请成功！具体生成情况，请及时关注“订单管理”菜单！',3000);
                getList()
            }else {
                $.Huimodalalert('申请失败！',2000);
            }

        },
        error: function () {
            $.Huimodalalert('申请失败！',2000);
        }
    })
}

//取消
function cancel(channelOrderId) {
    $.ajax({
        url:root.dockingplatform.cancelPreOrder,
        contentType: "application/json;charset=UTF-8",
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        data: JSON.stringify({"channelOrderId":channelOrderId}),
        success: function (data) {
            if(data.errorCode==0){
                $.Huimodalalert('取消成功！',2000);
                getList()
            }else {
                $.Huimodalalert('取消失败',2000);
            }

        },
        error: function () {
            $.Huimodalalert('取消失败！',2000);
        }
    })
}


function getList(){
        if ($('#ascription_lsit option:selected') .val()==""){

            layer.msg("请选择渠道商");
            return;
        }

    var per={}
            per = {
                pageNum: 1, //当前页码
                pageSize: 10,//每页显示条数
                "parentCustomerCode":$("#ascription_lsit").val(),//渠道商id
                "channelOrderId":$("#dorderCode").val(),
                "orderStatus":$("#exceptionType").val(),
                "createdAtStart":$("#timemin").val(), //下单时间
                "createdAtEnd":$("#timemax").val(),
                "pushAtStart":$("#tuisongmin").val(), //推送时间
                "pushAtEnd":$("#tuisongmax").val()
            };

    $.ajax({
        contentType: "application/json",
        type: "POST",
        url: root.dockingplatform.getFailedPreOrderList,
        cache: false,  //禁用缓存
        dataType: "json",
        data: JSON.stringify(per),
        success: function (result) {
            isIng = false;
            orderList(result);
            $('.M-box3').pagination({
                pageCount: result.data.totalPage,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    var perdd={}
                            perdd = {
                                pageNum: api.getCurrent(), //当前页码
                                pageSize: 10,//每页显示条数
                                "parentCustomerCode":$("#ascription_lsit").val(),//渠道商id
                                "channelOrderId":$("#dorderCode").val(),
                                "orderStatus":$("#exceptionType").val(),
                                "createdAtStart":$("#timemin").val(), //下单时间
                                "createdAtEnd":$("#timemax").val(),
                                "pushAtStart":$("#tuisongmin").val(), //推送时间
                                "pushAtEnd":$("#tuisongmax").val()
                            };
                    $.ajax({
                        contentType: "application/json",
                        type: "POST",
                        url:root.dockingplatform.getFailedPreOrderList,
                        cache: false,  //禁用缓存
                        dataType: "json",
                        data: JSON.stringify(perdd),
                        success: function (result) {
                            orderList(result);
                        },
                        error: function (result) {
                            $.Huimodalalert(result.data.reason,2000);
                        }
                    });
                }
            });
        },
        error: function (result) {
            $.Huimodalalert(result.data.reason,2000);
        }
    });
}
